/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswt;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * A general multi-purpose event listener 
 * @author Sam Pottinger
 */
public class SpaglEventListener implements Listener{
	
	private Method targetMethod;
	private Object targetObject;
	private int eventType;
	
	/**
	 * Creates an event listener that will execute a method on a given object when a given event is fired
	 * @param newTargetMethod The method ({@link java.lang.reflect.Method}) to be executed
	 * @param newTargetObject The object where the method should be excuted
	 * @param newEventType The library-specific integer event type.
	 */
	public SpaglEventListener(Method newTargetMethod, Object newTargetObject, int newEventType)
	{
		targetMethod = newTargetMethod;
		targetObject = newTargetObject;
		eventType = newEventType;
	}
	
	/**
	 * Fires this event's attached method
	 * @param e The event information to be passed on
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public void invokeEventMethod(SpaglEvent e)
	{
		try {
			targetMethod.invoke(targetObject, e);
		} catch (IllegalArgumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public void handleEvent(Event e) {
		invokeEventMethod(new SpaglEvent(e, SpaglEvent.SELECTION_EVENT));
	}
	
	/**
	 * Gets the type of event listener that this is
	 * @return An integer representation of this object's event type
	 */
	public int getEventType()
	{
		return eventType;
	}
}
